﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.MobileControls;
using System.IO;
using System.Drawing;

namespace ProfilKezelo
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ProfilKezelo : System.Web.Services.WebService
    {

        [WebMethod]
        public CWMOsztalyok.Profil Bejelentkezés(string email, string jelszó)
        {
            if (Azonosított(email, jelszó))
            {
                CWMOsztalyok.Profil sajátProfil = ProfilElkészit(email);
                List<string> ismerösök = IsmerősökÖsszegyüjtése(email);
                foreach (string ismi in ismerösök)
                {
                    CWMOsztalyok.Profil prof = ProfilElkészit(ismi);
                    if (prof != null)
                    {
                        sajátProfil.Kapcsolatok.Add(prof);
                    }
                }
                return sajátProfil;

            }
            else return null;
        }
        [WebMethod]
        public List<string> IsmerősökÖsszegyüjtése(string email)
        {
            List<string> res = new List<string>();
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["minerva"].ConnectionString);
            try
            {
                string commandtext = "SELECT partner2 FROM Kapcsolatok WHERE partner1 = '" + email + "'";
                SqlCommand cmd = new SqlCommand(commandtext, conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    res.Add(dr.GetString(0));
                }
                conn.Close();
                return res;


            }
            catch { }
            finally { conn.Close(); } return null;

        }
        [WebMethod]
        public CWMOsztalyok.Profil ProfilElkészit(string email)
        {
            CWMOsztalyok.Profil prof = new CWMOsztalyok.Profil(email);
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["minerva"].ConnectionString);
            try
            {
                string commandtext = "SELECT vezeteknev, keresztnev, szuletes, profilkep " +
                "FROM Felhasznalok WHERE email = '" + email + "'";
                SqlCommand cmd = new SqlCommand(commandtext, conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    prof.Vezeteknev = dr.GetString(0);
                    prof.Keresztnev = dr.GetString(1);
                    prof.Becenev = dr.GetString(1);
                    prof.SzuletesiIdo = dr.GetDateTime(2);
                    prof.Kapcsolatok = new List<CWMOsztalyok.Profil>();
                    //prof.ProfilKep
                }
                conn.Close();
                return prof;
            }
            catch { }
            finally { conn.Close(); } return null;
        }

        [WebMethod]
        public bool Azonosított(string email, string jelszó)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["minerva"].ConnectionString);
            try
            {
                string commandtext = "SELECT COUNT(email) FROM Felhasznalok WHERE email = '" + email + "' AND jelszo = '" + jelszó + "'";
                SqlCommand cmd = new SqlCommand(commandtext, conn);
                conn.Open();
                if ((int)cmd.ExecuteScalar() > 0)
                { conn.Close(); return true; }
            }
            catch { }
            finally { conn.Close(); }
            return false;
        }

        [WebMethod]
        public bool Regisztráció(string email, string jelszó, string vezeteknev, string keresztnev,DateTime? szuldatum)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["minerva"].ConnectionString);
            try
            {
                string commandtext = "INSERT INTO [Felhasznalok] ([email], [jelszo] ,[vezeteknev] ,[keresztnev], [szuletes]) VALUES ( '" + email + "' , '" + jelszó + "' , '" + vezeteknev + "' , '" + keresztnev + "' , '" + szuldatum + "' )";
                SqlCommand cmd = new SqlCommand(commandtext, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                return true; 
            }
            catch { }
            finally { conn.Close(); }
            return false;

        }

        [WebMethod]
        public bool UjKapcsolat(string sajatEmail, string partnerEmail)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["minerva"].ConnectionString);
            try
            {
                string commandtext = "INSERT INTO [Kapcsolatok] ([partner1], [partner2] ) VALUES ( '" + sajatEmail + "' , '" + partnerEmail + "' )";
                SqlCommand cmd = new SqlCommand(commandtext, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                 conn.Close();
                 commandtext = "INSERT INTO [Kapcsolatok] ([partner1], [partner2] ) VALUES ( '" + partnerEmail + "' , '" + sajatEmail + "' )";
                 cmd = new SqlCommand(commandtext, conn);
                 conn.Open();
                 cmd.ExecuteNonQuery();
                 conn.Close();
                return true; 
            }
            catch { }
            finally { conn.Close(); }
            return false;

        }

        [WebMethod]
        public Bitmap KepLekerdezes(string email)
        {
            MemoryStream stream = new MemoryStream();
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["minerva"].ConnectionString);

            try
            {
                conn.Open();
                SqlCommand command = new
                  SqlCommand("SELECT profilkep FROM Felhasznalok WHERE email = '" + email + "' ", conn);
                byte[] image = (byte[])command.ExecuteScalar();
                stream.Write(image, 0, image.Length);
                Bitmap bitmap = new Bitmap(stream);
                return bitmap;
            }
            catch { return null; }
            finally
            {
                conn.Close();
                stream.Close();                
            }
            

        }

        [WebMethod]
        public void KepFeltolt(byte[] kep, string email)
        {

            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["minerva"].ConnectionString);
            try
            {
                connection.Open();
               SqlCommand cmd = new SqlCommand("UPDATE Felhasznalok SET profilkep = @profilkep WHERE email = @email", connection);
                cmd.Parameters.Add("@profilkep", kep);
                cmd.Parameters.Add("@email",email);
                cmd.ExecuteNonQuery();
            }
            finally
            {
                connection.Close();
            }

 
        }


    }
}
